#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
struct Record
{
	string _word;
	int _frequency;
};

class Dictionary
{
public:
    bool is_word(const string & word){
        for(auto & letter : word){
            if(!isalpha(letter)){
               return false;
            }
        }
        return true;
    }
    void read(const std::string &filename){
       ifstream ifs;
       ifs.open(filename);
       if(!ifs.good()){
        cerr<<"ifs steam is not good"<<endl;
        return;
       }
       //先读取每一行的数据
       string line;
       while(getline(ifs,line)){
            string word;
            while(ifs >> word){
                //判断是否为单词
                if(is_word(word)){
                    //判断是否出现过
                    bool found = false;
                    for(auto & letter : _dict){
                        if(word == letter._word){
                            found = true;
                           ++letter._frequency;
                           break;
                        }
                    }
                    if(!found){
                        _dict.push_back(Record{word,1});
                    }
                }
            }
       }
    }
    void store(const std::string &filename){
        ofstream ofs(filename);
        auto it = _dict.begin();
        while(it != _dict.end()){
            ofs << it->_word << " " << it->_frequency <<endl;
            ++it;
        }
        ofs.close();
    }
private:
    vector<Record> _dict;
};
void test(){
    Dictionary dc;
    dc.read("The_Holy_Bible.txt");
    dc.store("dict.txt");
}
int main()
{
    test();
    return 0;
}

